/**
 * 
 */
package com.m7012e.a_remote.android_remote.network.protocol.echoProtocol;

import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.TreeSet;

import com.leoj.utils.Handler;
import com.leoj.utils.tcp_network.ApplicationProtocol;
import com.leoj.utils.tcp_network.ApplicationProtocolFactory;
import com.m7012e.a_remote.android_remote.network.protocol.echoProtocol.internEvents.DaemonFoundEvent;
import com.m7012e.a_remote.android_remote.network.protocol.echoProtocol.internEvents.FreeDaemonFoundEvent;
import com.m7012e.a_remote.communication.Daemon;

/**
 * @author leoj
 *
 */
public class DaemonListingProtocolFactory implements ApplicationProtocolFactory, Handler<DaemonFoundEvent>{
	private Set<DaemonFoundEvent> _daemonList;
	private SortedMap<Daemon, Integer> _availableDaemons;
	/* (non-Javadoc)
	 * @see com.leoj.utils.tcp_network.ApplicationProtocolFactory#createProtocol()
	 */
	@Override
	public ApplicationProtocol createProtocol() {
		return new DaemonListingProtocol(this);
	}

	/* (non-Javadoc)
	 * @see com.leoj.utils.Handler#handle(java.lang.Object)
	 */
	@Override
	public void handle(DaemonFoundEvent e) {
		System.out.println("new DaemonFoundEvent !");
		System.out.println("-> "+e);
		_daemonList.add(e);
		if(e.available){
			FreeDaemonFoundEvent freedaemon = (FreeDaemonFoundEvent) e;
			_availableDaemons.put(freedaemon.daemon, freedaemon.distance);
		}
	}
	public void startListing(){
		_daemonList = new TreeSet<DaemonFoundEvent>();
		_availableDaemons = new TreeMap<Daemon, Integer>();
	}
	public String printCurrentListing(){
		String str = "";
		for(DaemonFoundEvent e : _daemonList){
			str += e.toString()+"\n";
		}
		return str;
	}
	public SortedMap<Daemon, Integer> getCurrentAvailable(){
		return _availableDaemons;
	}
	public int getTotalAnswers(){
		return _daemonList.size();
	}
	public int getAvailableAnswers(){
		return _availableDaemons.size();
	}
}
